package Myxml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.sql.RowSetInternal;
import javax.sql.rowset.WebRowSet;
import javax.sql.rowset.spi.XmlReader;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

public class demo1 {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("D:\\software\\code\\2023_java\\2023_java_code\\code_24_4_28\\src\\Myxml\\text.xml");
        Element rootElement = document.getRootElement();
        String rootName = rootElement.getName();
        System.out.println("根目录名字:" + rootName);

        //拿到当前根目录下的全部一级目录
        List<Element> list = rootElement.elements();

        //拿到当前根目录下的指定的目录，根据名字来获取到标签对象
        //如果是相同名字，将会获取第一个标签对象
        Element element = rootElement.element("user");
        System.out.println(element.getName());

        //获取标签中的属性,获取所有属性
        List<Attribute> attributeList = element.attributes();
        for (Attribute attribute : attributeList) {
            System.out.print(attribute.getName() + " :" + attribute.getValue() + " ");
        }

        System.out.println();

        //获取标签中指定的属性
        Attribute attribute = element.attribute("id");
        System.out.println(attribute.getName() + " :" + attribute.getValue());


        //根据标签获取文本内容
        String s = element.elementText("name");
        System.out.println(s);

        //获取文本
        Element nameElement = element.element("name");
        String name = nameElement.getText();
        System.out.println(name);


    }

}
